<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\ding;

use app\api\controller\Controller;
use app\api\model\ding\Liebian as LiebianModel;
use app\common\enum\ding\Ribao as SceneEnum;
use think\facade\Db;
use think\response\Json;

/**
 * 用户预约管理
 * Class Cart
 * @package app\api\controller
 */
class Liebian extends Controller
{

    public function list(): Json
    {
        $service = new LiebianModel;
        $list    = $service->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    public function getStatics(int $shopId, $clerkId = 0, $startDate = '', $endDate = ''): Json
    {
        if ($startDate == '') {

            $startDate = Date('Y-m-01');
        } else {
            $startDate = Date('Y-m-d', strtotime($startDate));
        }
        if ($endDate == '') {
            $endDate = Date('Y-m-d');
        } else {
            $endDate = Date('Y-m-d', strtotime($endDate));
        }
        $service = new LiebianModel;
        $shop    = Db::name('store_shop')->where('shop_id', $shopId)->find();
        $where[] = ['shop_id', '=', $shopId];
        if ($clerkId > 0) {
            $where[] = ['clerk_id', '=', $clerkId];
        } else {
            $where[] = ['clerk_id', '=', 0];
        }
        // $where[] = ['date', '>=', $startDate];
        // $where[] = ['date', '<=', $endDate];
        //只显示当前月的数据
        $time    = strtotime(Date('Y-m-01'));
        $where[] = ['create_time', '>=', $time];
        //门店曝光
        $statics['shop_views'] = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::MDBGL)->sum('value');

        $statics['yxdd_order'] = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::YXDD)->sum('value');
        $statics['qxdd_order'] = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::QXDD)->sum('value');

        //裂变用户
        $statics['liebian_member'] = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::LBYHS)->sum('value');
        //邀请函转化
        $statics['zhuanhua_member'] = Db::name('ding_shop_liebian')->where('status', 1)
            ->where('shop_id', $shopId)->count();
        //邀请函曝光量
        $statics['card_views']     = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::YQHBGL)->sum('value');
        $statics['duozheng_money'] = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::DZ)->sum('value');
        $statics['save_money']     = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::JS)->sum('value');
        $statics['sunshi_money']   = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::SS)->sum('value');

        $statics['yeji'] = $statics['zhuanhua_member'] * 500;

        $startDate = Date('Y.m.d', strtotime($startDate));
        $endDate   = Date('Y.m.d', strtotime($endDate));
        return $this->renderSuccess(compact('statics', 'startDate', 'endDate'));
    }

    //每日日报

    public function getRibao(int $shopId, $clerkId = 0, $startDate = ''): Json
    {
        if ($startDate == '') {

            $startDate = Date('Y-m-d', time() - 86400);
        } else {
            $startDate = Date('Y-m-d', strtotime($startDate));
        }

        $service = new LiebianModel;
        $shop    = Db::name('store_shop')->where('shop_id', $shopId)->find();
        $where[] = ['shop_id', '=', $shopId];
        if ($clerkId > 0) {
            $where[] = ['clerk_id', '=', $clerkId];
        } else {
            $where[] = ['clerk_id', '=', 0];
        }
        $where[] = ['date', '=', $startDate];

        //门店曝光
        $statics['shop_views'] = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::MDBGL)->sum('value');

        $statics['yxdd_order'] = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::YXDD)->sum('value');
        $statics['qxdd_order'] = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::QXDD)->sum('value');

        //裂变用户
        $statics['liebian_member'] = Db::name('ding_ribao')
            ->where($where)
            ->where('scene', SceneEnum::LBYHS)->sum('value');

        $statics['zhuanhua_member'] = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::YQHLBL)->sum('value');
        //邀请函曝光量
        $statics['card_views']     = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::YQHBGL)->sum('value');
        $statics['duozheng_money'] = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::DZ)->sum('value');
        $statics['save_money']     = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::JS)->sum('value');
        $statics['sunshi_money']   = Db::name('ding_ribao')->where($where)->where('scene', SceneEnum::SS)->sum('value');

        $statics['yeji'] = $statics['zhuanhua_member'] * 500;

        $startTime = strtotime($startDate);
        $endTime   = $startTime + 86400;

        //个人名片访问量
        $personl['visit_views'] = Db::name('ding_visit_log')
            ->where('source_id', 1)
            ->where('clerk_id', $clerkId)
            ->where('create_time', '>=', $startTime)->where('create_time', '<', $endTime)->count();
        $personl['visit_order_views'] = Db::name('ding_order')->where('clerk_id', $clerkId)
            ->where('create_time', '>=', $startTime)->where('create_time', '<', $endTime)->where('source_id', 1)->count();
        $personl['copy_views'] = Db::name('ding_friend_copy_log')->where('clerk_id', $clerkId)
            ->where('create_time', '>=', $startTime)->where('create_time', '<', $endTime)->count();

        $personl['friend_visit_views'] = Db::name('ding_visit_log')
            ->where('source_id', 3)->where('clerk_id', $clerkId)
            ->where('create_time', '>=', $startTime)->where('create_time', '<', $endTime)->count();

        $yeji = Db::name('ding_order')->where('clerk_id', $clerkId)
            ->where('create_time', '>=', $startTime)->where('create_time', '<', $endTime)
            ->where('status', '<>', 30)

            ->count();
        $rank = 0;
        if ($yeji > 0) {
            //排名
            $clerks = Db::name('store_shop_clerk')->where('shop_id', $shopId)->where('is_delete', 0)->where('status', 1)
                ->field('clerk_id')->select()->toArray();
            foreach ($clerks as $key => $value) {
                $count_order = Db::name('ding_order')->where('clerk_id', $value['clerk_id'])
                    ->where('create_time', '>=', $startTime)->where('create_time', '<', $endTime)
                    ->where('status', '<>', 30)
                    ->count();
                $clerks[$key]['count'] = $count_order;
            }
            $age  = array_column($clerks, 'clerk_id');
            $name = array_column($clerks, 'count');
            array_multisort($age, SORT_ASC, $name, SORT_ASC, $clerks);

            foreach ($clerks as $key => $value) {
                if ($value['clerk_id'] == $clerkId) {
                    $rank = $key + 1;
                }
            }

        }

        $personl['rank']       = $rank;
        $personl['order_yeji'] = $yeji;

        $startDate = Date('Y.m.d', strtotime($startDate));

        return $this->renderSuccess(compact('statics', 'startDate', 'personl'));
    }

    //用户预约
    public function add(): Json
    {
        $model = new LiebianModel;
        $id    = $model->add($this->postForm());
        return $this->renderSuccess([], '您的预订已提交给您的专属服务经理!');
    }

}
